<?php

class MoveDetailController extends BackendApplicationController {

	public $layout = 'application.modules.delivery.views.layouts.delivery';
	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'DeliveryMoveDetailModel'),
		));
	}

	public function actionCreate() {
		$model = new DeliveryMoveDetailModel;


		if (isset($_POST['DeliveryMoveDetailModel'])) {
			$model->setAttributes($_POST['DeliveryMoveDetailModel']);
			$model->created_datetime = new CDbExpression('NOW()');
			if ($model->save()) {
				$model->setAttribute('code', 'D'.$model->id);
				$model->save();
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}
		$ticket='';
		if(Yii::app()->user->hasState('ticket')){
			$ticket = Yii::app()->user->getState('ticket');
		}
		$this->render('create', array( 
				'model' => $model,
				'ticket'=>$ticket,
		));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'DeliveryMoveDetailModel');


		if (isset($_POST['DeliveryMoveDetailModel'])) {
			$model->setAttributes($_POST['DeliveryMoveDetailModel']);

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}
		$ticket=$model->order_ticket_id;
		$this->render('update', array(
				'model' => $model,
				'ticket'=> $ticket
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'DeliveryMoveDetailModel')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('DeliveryMoveDetailModel');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new DeliveryMoveDetailModel('search');
		$model->unsetAttributes();
		if(isset($_GET['id']) && !empty($_GET['id'])){
			Yii::app()->user->setState('ticket', $_GET['id']);
		}
		$ticket='';
		if(Yii::app()->user->hasState('ticket')){
			$ticket = Yii::app()->user->getState('ticket');
		}
		if (isset($_GET['DeliveryMoveDetailModel']))
			$model->setAttributes($_GET['DeliveryMoveDetailModel']);

		$this->render('admin', array(
			'model' => $model,
			'ticket'=>$ticket
		));
	}

}